const fetch = require('node-fetch');

// 管理员登录获取token
async function getAdminToken() {
  try {
    console.log('尝试登录...');
    const response = await fetch('http://localhost:3000/api/auth/login', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({
        email: 'admin@campus.edu',
        password: 'Admin123!'
      })
    });

    console.log('登录响应状态:', response.status);
    const responseText = await response.text();
    console.log('登录响应:', responseText);

    if (!response.ok) {
      throw new Error(`Login failed: ${response.status} - ${responseText}`);
    }

    const data = JSON.parse(responseText);
    return data.token;
  } catch (error) {
    console.error('获取管理员token失败:', error);
    throw error;
  }
}

// 测试徽章记录API
async function testBadgeRecordsAPI() {
  try {
    const token = await getAdminToken();
    console.log('获取到管理员token:', token);

    // 测试徽章记录API
    const response = await fetch('http://localhost:3000/api/badges/records?page=1&limit=10', {
      headers: {
        'Authorization': `Bearer ${token}`
      }
    });

    console.log('API响应状态:', response.status, response.statusText);
    
    if (!response.ok) {
      const error = await response.text();
      throw new Error(`API请求失败: ${error}`);
    }

    const data = await response.json();
    console.log('徽章记录数据:', JSON.stringify(data, null, 2));
  } catch (error) {
    console.error('测试徽章记录API失败:', error);
  }
}

// 运行测试
testBadgeRecordsAPI(); 